from appium import webdriver
import time

# 中国移动
def yidong(driver):
  print('移动开始: ' + time.strftime("%H:%M:%S", time.localtime()))
  driver.start_activity('com.kingpoint.gmcchh','.newui.main.skeleton.view.SkeletonActivity')
  time.sleep(8)
  # 签到
  if len(driver.find_elements_by_id('com.kingpoint.gmcchh:id/iv_mailBoxDialogClose')) >= 1:
    driver.find_element_by_id('com.kingpoint.gmcchh:id/iv_mailBoxDialogClose').click()
  driver.find_element_by_id("com.kingpoint.gmcchh:id/tvMineToolbarTitle").click()
  driver.find_element_by_id("com.kingpoint.gmcchh:id/iv_membership_grade").click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[2]/android.widget.Image').click()
  print('移动完成: ' + time.strftime("%H:%M:%S", time.localtime()))
# 中国石化
def shihua(driver):
  print('石化开始: ' + time.strftime("%H:%M:%S", time.localtime()))
  driver.start_activity('com.sunboxsoft.oilforgdandroid','net.sourceforge.simcpux.activity.MainActivity')
  time.sleep(7)
  # 我的
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View[2]/android.view.View/android.view.View[3]/android.view.View[5]/android.widget.ListView/android.view.View[4]/android.view.View/android.view.View[3]').click()
  time.sleep(1)
  # 签到
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[2]/android.widget.Image').click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[2]/android.view.View[1]/android.view.View[2]/android.view.View/android.widget.Image').click()
  print('石化完成: ' + time.strftime("%H:%M:%S", time.localtime()))

# 京东APP
def jingdong(driver):
  print('京东开始: ' + time.strftime("%H:%M:%S", time.localtime()))
  # driver.start_activity('com.jingdong.app.mall','.main.MainActivity')
  time.sleep(3)
  # 签到
  driver.find_element_by_xpath('//androidx.recyclerview.widget.RecyclerView[@content-desc="NewAppcenter"]/android.widget.RelativeLayout[7]').click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup[1]/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup/android.widget.TextView').click()
  time.sleep(2)
  driver.back()

  # 农场
  driver.find_element_by_xpath('//androidx.recyclerview.widget.RecyclerView[@content-desc="NewAppcenter"]/android.widget.RelativeLayout[4]').click()
  time.sleep(3)
  # 广告
  if len(driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]')) >= 1:
    driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]').click()
  # 浇水
  for i in range(11):
    driver.tap([(756, 1555), (1044, 1860)], 500) 	
    time.sleep(3)
    if len(driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]/android.widget.Image')) >= 1:
      driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]/android.widget.Image').click()
      time.sleep(3)
      if len(driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]')) >= 1:
        driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]').click()
  # 签到农场
  time.sleep(1)
  driver.tap([(55, 1626), (220, 1833)], 500) 	
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[2]/android.view.View[5]').click()
  time.sleep(2)
  driver.back()
  # 领取水滴
  driver.tap([(217, 1626), (380, 1833)], 500) 	  	
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[2]/android.view.View[1]/android.view.View[6]/android.view.View[2]').click()
  # 浏览领水滴s
  time.sleep(1)
  driver.back()
  time.sleep(1)
  driver.back()
  driver.find_element_by_xpath('//androidx.recyclerview.widget.RecyclerView[@content-desc="NewAppcenter"]/android.widget.RelativeLayout[4]').click()
  time.sleep(2)
  # 广告
  if len(driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]')) >= 1:
    driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]').click()
  driver.tap([(217, 1626), (380, 1833)], 500)     
  for i in range(2):
    driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View[4]/android.view.View[2]').click()
    time.sleep(8)
    driver.back()
    driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View[4]/android.view.View[2]').click()

  print('京东完成: ' + time.strftime("%H:%M:%S", time.localtime()))

# 京东金融
def jingdongjinrong(driver):
  print('京东金融开始: ' + time.strftime("%H:%M:%S", time.localtime()))
  driver.start_activity('com.jd.jrapp','.bm.mainbox.main.MainActivity')
  time.sleep(3)
  # 签到
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[2]/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout[5]/android.view.ViewGroup').click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.tbs.core.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[2]/android.view.View/android.view.View[8]').click()
  print('京东金融完成: ' + time.strftime("%H:%M:%S", time.localtime()))

# 拼多多
def pinduoduo(driver):
  print('拼多多开始: ' + time.strftime("%H:%M:%S", time.localtime()))
  # driver.start_activity('com.xunmeng.pinduoduo','.ui.activity.MainFrameActivity')
  time.sleep(3)
  # 签到
  driver.find_element_by_xpath('//android.widget.FrameLayout[@content-desc="home_icon_set"]/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[8]').click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout/meco.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View[1]').click()
  driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout/meco.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[2]/android.view.View[3]/android.view.View[1]/android.view.View').click()
  print('拼多多完成: ' + time.strftime("%H:%M:%S", time.localtime()))

desired_caps = {
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '10', # 手机安卓版本
  'deviceName': 'onePlus', # 设备名，安卓手机可以随意填写
  'appPackage': 'com.jingdong.app.mall', # 启动APP Package名称
  'appActivity': '.main.MainActivity', # 启动Activity名称
#   'unicodeKeyboard': True, # 使用自带输入法，输入中文时填True
#   'resetKeyboard': True, # 执行完程序恢复原来输入法
  'noReset': True,       # 不要重置App
  'newCommandTimeout': 6000,
  'autoGrantPermissions': True,
  'automationName' : 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}

def main():
  # 连接Appium Server，初始化自动化环境
  driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
  # 设置缺省等待时间
  driver.implicitly_wait(5)
  jingdong(driver)
  print('-------------------------------')
  yidong(driver)
  print('-------------------------------')
  shihua(driver)
  print('-------------------------------')
  jingdongjinrong(driver)
  print('-------------------------------')
  input('**** Press to quit..')
  driver.quit()

if __name__ == '__main__':
    main()